<template>
	<view class="homeLayout pageBg">
		<custom-nav-bar></custom-nav-bar>
		<up-sticky bgColor="#F8F8FF" :offset-top="getStatusBarHeight()">
			<up-tabs :list="tabList" :current="current" lineWidth="80" lineColor="#FF4D4F"
				itemStyle="padding-left: 40px; padding-right: 40px; height: 40px;" :activeStyle="{
				    color: '#FF4D4F',
				    fontWeight: 550,
				    transform: 'scale(1.05)'
				}" :inactiveStyle="{
				    color: '#999',
				    transform: 'scale(1)'
				}" @click="tabChange">
			</up-tabs>
		</up-sticky>

		<view v-if="current == 0" class="recommend">
			<view class="banner">
				<swiper indicator-dots="true" indicator-color="rgba(255,255,255,0.5)" indicator-active-color="#fff"
					autoplay="true" circular="true">
					<swiper-item v-for="item in 4">
						<image :src="getImage('/banner/banner1.jpg')" mode="aspectFill"></image>
					</swiper-item>
				</swiper>
			</view>

			<view class="item">
				<view class="box" v-for="(recommend, index) in recommendList" :key="index"
					@click="goPost(recommend.dishId)">
					<view class="item-content">
						<image :src="getImage2(recommend.imageUrl)" mode="aspectFill" class="thumbnail"></image>
						<view class="count-info">
							<view class="counts">
								<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(recommend.views) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(recommend.likes) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(recommend.favorites) }}</text>
							</view>
						</view>
						<view class="item-info">
							<view class="title">
								<up-text :lines="2" :text="recommend.name" size="16"></up-text>
							</view>
							<view class="user">
								<uni-icons type="person" color="#888" size="18"></uni-icons>
								<text>{{ recommend.username }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<up-gap height="10"></up-gap>
			<up-loadmore :line="true" status="nomore"></up-loadmore>
			<up-gap height="70"></up-gap>
			<up-back-top :scroll-top="scrollTop" :iconStyle="iconStyle" :customStyle="customStyle"></up-back-top>
		</view>

		<view v-else-if="current == 1" class="hot">
			<view class="item">
				<view class="box" v-for="(hot, index) in hotList" :key="index" @click="goPost(hot.dishId)">
					<view class="item-content">
						<image :src="getImage2(hot.imageUrl)" mode="aspectFill" class="thumbnail"></image>
						<view class="count-info">
							<view class="counts">
								<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(hot.views) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(hot.likes) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(hot.favorites) }}</text>
							</view>
						</view>
						<view class="item-info">
							<view class="title">
								<up-text :lines="2" :text="hot.name" size="16"></up-text>
							</view>
							<view class="user">
								<uni-icons type="person" color="#888" size="18"></uni-icons>
								<text>{{ hot.username }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<up-gap height="10"></up-gap>
			<up-loadmore :line="true" status="nomore"></up-loadmore>
			<up-gap height="70"></up-gap>
			<up-back-top :scroll-top="scrollTop" :iconStyle="iconStyle" :customStyle="customStyle"></up-back-top>
		</view>

		<view v-else-if="current == 2" class="follow">
			<view class="item">
				<view class="box" v-for="(follow, index) in followList" :key="index" @click="goPost(follow.dishId)">
					<view class="item-content">
						<image :src="getImage2(follow.imageUrl)" mode="aspectFill" class="thumbnail"></image>
						<view class="count-info">
							<view class="counts">
								<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(follow.views) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(follow.likes) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(follow.favorites) }}</text>
							</view>
						</view>
						<view class="item-info">
							<view class="title">
								<up-text :lines="2" :text="follow.name" size="16"></up-text>
							</view>
							<view class="user">
								<uni-icons type="person" color="#888" size="18"></uni-icons>
								<text>{{ follow.username }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<up-gap height="10"></up-gap>
			<up-loadmore :line="true" status="nomore"></up-loadmore>
			<up-gap height="70"></up-gap>
			<up-back-top :scroll-top="scrollTop" :iconStyle="iconStyle" :customStyle="customStyle"></up-back-top>
		</view>

		<view v-else class="classify">
			<view class="item">
				<view class="box" v-for="(classify, index) in classifyData" :key="index"
					@click="goPost(classify.dishId)">
					<view class="item-content">
						<image :src="getImage2(classify.imageUrl)" mode="aspectFill" class="thumbnail"></image>
						<view class="count-info">
							<view class="counts">
								<uni-icons type="eye" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(classify.views) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="hand-up" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(classify.likes) }}</text>
							</view>
							<view class="counts">
								<uni-icons type="star" color="#ffffff" size="18"></uni-icons>
								<text>{{ format(classify.favorites) }}</text>
							</view>
						</view>
						<view class="item-info">
							<view class="title">
								<up-text :lines="2" :text="classify.name" size="16"></up-text>
							</view>
							<view class="user">
								<uni-icons type="person" color="#888" size="18"></uni-icons>
								<text>{{ classify.username }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
			<up-gap height="10"></up-gap>
			<up-loadmore :line="true" status="nomore"></up-loadmore>
			<up-gap height="70"></up-gap>
			<up-back-top :scroll-top="scrollTop" :iconStyle="iconStyle" :customStyle="customStyle"></up-back-top>
		</view>
		<up-popup :show="showPopup" mode="top" @close="closePopup" @open="openPopup">
			<view class="classify-item" :style="'padding-top:'+getStatusBarHeight()+'px'">
				<view class="classify-item-title">
					<view>选择类别</view>
				</view>
				<view class="classify-item-name" v-for="(item,index) in classifyList" :key="index"
					@click="selectClassify(item.name)">
					{{item.name}}
				</view>
			</view>
		</up-popup>
	</view>

</template>

<script setup>
	import {
		ref,
		createApp,
		onMounted,
		reactive,
		watch
	} from 'vue';
	import {
		onShow,
		onReachBottom,
		onPullDownRefresh
	} from '@dcloudio/uni-app' // 导入uniapp页面生命周期钩子
	import {
		getStatusBarHeight,
	} from '@/utils/system.js'
	import {
		appUrl
	} from '../../main';

	import {
		onPageScroll
	} from '@dcloudio/uni-app';

	const showPopup = ref(false);

	function openPopup() {
		showPopup.value = true;
	}

	function closePopup() {
		showPopup.value = false;
	}

	//数据
	let recommendList = ref([]);
	let hotList = ref([]);
	let followList = ref([]);
	let classifyData = ref([]);
	const classifyList = reactive([{
			name: '家常菜'
		}, {
			name: '煎'
		}, {
			name: '蒸'
		}, {
			name: '炖'
		}, {
			name: '红烧'
		},
		{
			name: '炸'
		}, {
			name: '卤'
		}, {
			name: '干锅'
		}, {
			name: '火锅'
		}, {
			name: '免烤'
		}
	]);


	const selectClassify = async (e) => {
		try {
			uni.showToast({
				icon: 'loading',
				title: "加载中……",
				duration: 10000
			})
			uni.request({
				url: appUrl + '/dish/category',
				data: {
					category: e
				},
				method: "POST"
			}).then((res) => {
				if (res.data.status == 200) {
					classifyData.value = res.data.data;
					showPopup.value = false;
					uni.showToast({
						title: "加载成功"
					})
				} else {
					console.log('服务异常');
				}
			})
		} catch (error) {
			console.error('请求失败:', error);
		}
	}

	// 创建响应式数据 scrollTop  
	const scrollTop = ref(0);

	// 使用 reactive 创建响应式对象数据  
	const iconStyle = reactive({
		color: '#ffffff'
	});
	const customStyle = reactive({
		background: '#FF4D4F'
	});

	// onPageScroll 方法来更新 scrollTop 的值  
	onPageScroll((e) => {
		scrollTop.value = e.scrollTop;
	});

	// 创建响应式数据  
	const tabList = reactive([{
			name: '推荐'
		},
		{
			name: '热门'
		},
		{
			name: '关注'
		},
		{
			name: '分类'
		},
	]);
	let current = ref(0)


	// 定义方法  
	const tabChange = (item) => {
		if (item.index == 0) {
			console.log('item', item);
			recommendPage.value = 1;
			recommendList.value = [];
			getRecommendList();
		} else if (item.index == 1) {
			console.log('item', item);
			hotPage.value = 1;
			hotList.value = [];
			getHotList();
		} else if (item.index == 2) {
			console.log('item', item);
			getFollowList();
		} else {
			console.log('item', item);
			showPopup.value = true;
		}
		current.value = item.index
	}

	const getImage = (path) => {
		return "https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/image" + path
	}
	const getImage2 = (path) => {
		return "https://xiaoxiaobaiyang.oss-cn-shenzhen.aliyuncs.com/food_moment_discover/train/data/" + path
	}

	const format = (num) => {
		if (num >= 100000) {
			return Math.round(num / 10000) + "w"
		} else if (num >= 10000) {
			return (Math.round(num / 1000)) / 10 + "w"
		} else {
			return num
		}
	}

	let hotPage = ref(1);
	const getHotList = async () => {
		try {
			uni.request({
				url: appUrl + '/dish/Hotdish',
				data: {
					page: hotPage.value,
					limit: 10
				},
				method: "POST"
			}).then((res) => {
				if (res.data.status == 200) {
					hotList.value = hotList.value.concat(res.data.data.dishes);
					hotPage.value = hotPage.value + 1;
				} else {
					console.log('服务异常');
				}
			})
		} catch (error) {
			console.error('请求失败:', error);
		}
	}

	const token = uni.getStorageSync("token");
	const getFollowList = async () => {
		try {
			uni.showToast({
				icon: 'loading',
				title: "加载中……",
				duration: 10000
			})
			uni.request({
				url: appUrl + '/follow/list',
				header: {
					'token': token
				},
				method: "GET"
			}).then((res) => {
				if (res.data.status == 200) {
					followList.value = res.data.data
					uni.showToast({
						title: "加载成功"
					})
				} else {
					uni.showToast({
						title: "加载成功"
					})
					token.value = uni.getStorageSync("token")
					if(token.value === null || token.value === ""){
						uni.showToast({
							icon:'error',
							title:"用户未登录",
							duration: 1000
						})
						uni.reLaunch({
							url:"/pages/UserView/UserView"
						})
					}else{
						uni.showToast({
							title: "加载失败"
						})
					}
				}
			})
		} catch (error) {
			console.error('请求失败:', error);
		}
	}

	let recommendPage = ref(1);
	const getRecommendList = async () => {
		try {
			uni.request({
				url: appUrl + '/dish/PersonDish',
				header: {
					'token': token
				},
				data: {
					page: recommendPage.value,
					limit: 10
				},
				method: "POST"
			}).then((res) => {
				if (res.data.status == 200) {
					recommendList.value = recommendList.value.concat(res.data.data.dishes);
					recommendPage.value = recommendPage.value + 1;
				} else {
					console.log('服务异常');
				}
			})
		} catch (error) {
			console.error('请求失败:', error);
		}
	}

	const goPost = (id) => {
		uni.setStorageSync('dishId', id)
		uni.navigateTo({
			url: "/pages/PostView/PostView"
		})
	}


	onShow(() => {
		getRecommendList();
		getHotList();
	})
	onMounted(() => {
		getFollowList();
	})
	onReachBottom(() => {
		getRecommendList();
		getHotList();
	})
	onPullDownRefresh(() => {
		recommendPage.value = 1;
		recommendList.value = [];
		hotPage.value = 1;
		hotList.value = [];
		getRecommendList();
		getHotList();
		setTimeout(function() {
			uni.stopPullDownRefresh();
		}, 1000);
	})
</script>

<style lang="scss" scoped>
	.homeLayout {
		height: 120vh;

		.banner {
			width: 750rpx;
			padding: 30rpx 0;

			swiper {
				width: 750rpx;
				height: 340rpx;

				&-item {
					width: 100%;
					height: 100%;
					padding: 0 30rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 10rpx;
					}
				}
			}
		}

		.classify-item {
			padding: 30rpx;
		}

		.classify-item-title {
			display: flex;
			justify-content: space-between;
			font-size: 18px;
			padding-bottom: 20rpx;
		}

		.classify-item-name {
			padding: 4rpx 24rpx;
			background-color: #F8F8F8;
			display: inline-block;
			border-radius: 26rpx;
			margin: 10rpx;
			color: #888;
		}

		.item {
			margin-top: 30rpx;
			padding: 0 30rpx;
			display: grid;
			grid-template-columns: 1fr 1fr;
			column-gap: 20rpx;
			row-gap: 20rpx;

			.box {
				height: 480rpx;
				border-radius: 12rpx;
				overflow: hidden;
				position: relative;
				background-color: #ffffff;

				.count-info {
					position: absolute;
					bottom: 155rpx;
					left: 0;
					display: flex;
					align-items: center;
					padding: 10rpx;
					background-color: rgba(0, 0, 0, 0);
					color: white;
					font-size: 13px;
					gap: 18rpx;

					.counts {
						display: flex;
						gap: 4rpx;
						align-items: center;
					}
				}

				.thumbnail {
					width: 100%;
					height: 320rpx;
				}

				.item-info {
					margin-left: 10rpx;

					.title {
						font-size: 16px;
						color: #333;
						height: 100rpx;
					}

					.user {
						display: flex;
						font-size: 14px;
						color: #999;
						gap: 10rpx;
						align-items: center;
					}
				}
			}
		}
	}
</style>